package com.xxx.servlet;

import com.alibaba.fastjson.JSON;
import com.xxx.pojo.ClassInfo;
import com.xxx.pojo.PageTools;
import com.xxx.service.Services;
import com.xxx.service.impl.ClassInfoServiceImpl;
import com.xxx.util.GetIdTime;
import com.xxx.util.JsonSwitchUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;

@WebServlet("/classInfo/*")
public class ClassInfoServlet extends BaseServlet {

    private Services service = new ClassInfoServiceImpl();

    public void selectAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        req.setCharacterEncoding("utf-8");
        BufferedReader bufferedReader = req.getReader();
        String readLine = bufferedReader.readLine();
        PageTools pageTools = JSON.parseObject(readLine, PageTools.class);
        List<ClassInfo> classInfos = service.selectAll((pageTools.getPageNo()-1)*pageTools.getPageSize(), pageTools.getPageSize());
//        System.out.println(classInfos);
        JsonSwitchUtil.toJsonString(req,resp,classInfos);

    }

    public void addClassInfo(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        BufferedReader reader = req.getReader();
        String line = reader.readLine();
        ClassInfo classInfo= JSON.parseObject(line, ClassInfo.class);

        Long id = (Long) req.getSession().getAttribute("id");

        String createTime = GetIdTime.getDataTime();

        classInfo.setUpdateTime(createTime);
        classInfo.setCreateTime(createTime);
        classInfo.setUpdateUser(id);
        classInfo.setCreateUser(id);
        System.out.println(classInfo);
        int add = service.add(classInfo);
        JsonSwitchUtil.toJsonString(req,resp,add);

    }

    public void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        BufferedReader reader = req.getReader();
        Long order = Long.valueOf(reader.readLine());
        int delete = service.delete(order);
        JsonSwitchUtil.toJsonString(req,resp,delete);
    }

    public void selectByOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        BufferedReader reader = req.getReader();
        Long order = Long.valueOf(reader.readLine());
        ClassInfo classInfo = (ClassInfo) service.selectById(order);

        JsonSwitchUtil.toJsonString(req,resp,classInfo);

    }

    public void updateByOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        req.setCharacterEncoding("UTF-8");

        BufferedReader reader = req.getReader();
        String line = reader.readLine();
        ClassInfo classInfo = JSON.parseObject(line, ClassInfo.class);

//        Long order = Long.valueOf(req.getParameter("order"));
//        String types = req.getParameter("types");
//        String typesName = req.getParameter("typesName");
        String updateTime = GetIdTime.getDataTime();

//        ClassInfo classInfo = new ClassInfo();
//
//        classInfo.setOrder(order);
//        classInfo.setTypes(types);
//        classInfo.setTypesName(typesName);
        classInfo.setUpdateTime(updateTime);
        classInfo.setUpdateUser((Long) req.getSession().getAttribute("id"));

        int i = service.updateById(classInfo);
        JsonSwitchUtil.toJsonString(req,resp,i);
    }

    public void selectCount(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        int i = service.selectCount();
        JsonSwitchUtil.toJsonString(req,resp,i);
    }

    public void selectAllByTypesName(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        List list = service.selectAllByTypesName();

        //System.out.println(list);
        JsonSwitchUtil.toJsonString(req,resp,list);

    }
}
